package prueba1;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.awt.image.BufferedImage;

import javax.swing.ImageIcon;
import javax.swing.JPanel;

import prueba2.DragPictureDemo;

class DisplayCanvas extends JPanel {
	  /**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	int x, y;
	boolean clickOutsideImage;
	int xo,yo=0;
	
	int fx, fy=0;

	  BufferedImage bi;

	  DisplayCanvas() {
	    setBackground(Color.white);
	    setSize(450, 400);
	    addMouseMotionListener(new MouseMotionHandler());
	    addMouseListener(new MouseAdapter() {
							public void mousePressed(MouseEvent evt) {
									imageMousePressed(evt);
							}
							public void mouseReleased(MouseEvent evt){
								imageMouseReleased(evt);
							}});
	    
	    Image image = createImageIcon("/Imagenes/img0356.gif").getImage();

	    MediaTracker mt = new MediaTracker(this);
	    mt.addImage(image, 1);
	    try {
	      mt.waitForAll();
	    } catch (Exception e) {
	      System.out.println("Exception while loading image.");
	    }

	    if (image.getWidth(this) == -1) {
	      System.out.println("no gif file");
	      System.exit(0);
	    }

	    bi = new BufferedImage(image.getWidth(this), image.getHeight(this),
	        BufferedImage.TYPE_INT_RGB);
	    Graphics2D big = bi.createGraphics();
	    big.drawImage(image, 0, 0, this);
	  }
	  
	  protected void imageMouseReleased(MouseEvent evt) 
	  {
		  if (inImage(evt.getX(),evt.getY()))
		  {
			  xo =(x);
			  yo = (y);	
		  }
	  }

	private void imageMousePressed(MouseEvent evt) 
	{
		if (inImage(evt.getX(),evt.getY()))
		{
			fx = evt.getX();
			fy = evt.getY();
		} 
	}
	
	private boolean inImage(int xx, int yy)
	{
		if(xx<x)
		{
			clickOutsideImage=true;
			return false;
		}	
		if(yy<y)
		{
			clickOutsideImage=true;
			return false;
		}
		if(xx>(x+bi.getWidth()))
		{
			clickOutsideImage=true;
			return false;
		}
		if(yy>(y+bi.getHeight()))
		{
			clickOutsideImage=true;
			return false;
		}
		  
		clickOutsideImage = false;
		return true;
	}
	  
	  public void paintComponent(Graphics g) 
	  {
		  super.paintComponent(g);
		  Graphics2D g2D = (Graphics2D) g;
		  g2D.drawImage(bi, x, y, this);
	  }

	  public ImageIcon createImageIcon(String path) 
	  {
		  java.net.URL imageURL = DragPictureDemo.class.getResource(path);
		  
		  if (imageURL == null) 
		  {
		      System.err.println("Resource not found: " + path);
		      return null;
		  } 
		  else
		  {
		      return new ImageIcon(imageURL);
		  }
	  }
	  
	  class MouseMotionHandler extends MouseMotionAdapter 
	  {	
		  public void mouseDragged(MouseEvent evt) 
		  {
			  if (!clickOutsideImage)
			  {
				  x = xo+(evt.getX()-fx);
				  y = yo+(evt.getY()-fy);
				  repaint();
			  }
		  }
	  }
}
